package com.jiantong.web.open.app.version;

import com.google.common.collect.Maps;
import com.jiantong.version.model.VersionControl;
import com.jiantong.version.service.VersionControlReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * Created by niufei on 2017/4/26.
 *
 */
@Slf4j
@OpenBean
public class VersionControlApis {

    @RpcConsumer
    private VersionControlReadService versionControlReadService;


    @OpenMethod(
            key = "check.version",
            paramNames = {"version","type"},
            httpMethods = RequestMethod.GET
    )
    public Map<String,Object> checkVersion(String version , Integer type){
        Map<String,Object> map = Maps.newHashMap();

        Response<VersionControl> versionControlResponse = versionControlReadService.findByType(type);
        if(!versionControlResponse.isSuccess()){
            log.error("failed to find VersionControl , cause : {}", versionControlResponse.getError());
            throw new OPServerException(versionControlResponse.getError());
        }

        if(version.equals(versionControlResponse.getResult().getVersionName())){
            map.put("action",false);
        }else {
            map.put("action",true);
            map.put("url",versionControlResponse.getResult().getUrl());
            map.put("description",versionControlResponse.getResult().getDescription());
        }
        return map;
    }
}
